<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\CourseTable;
use App\Admin\Repositories\CourseChapter;
use App\Models\Course;
use App\Models\CourseChapterNumber;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CourseChapterController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CourseChapter(['course:id,name','number:id,name']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('video_poster')->image('',100,80);
            $grid->column('number.name','次序');
            $grid->column('course.name','所属课程');
            $grid->column('name');
            $grid->column('is_free')
                 ->using([0 => '否',1 => '是'])
                 ->label([0 => 'danger',1 => 'success']);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('name')
                        ->width(3);
                $filter->equal('course_id','课程')
                    ->selectTable(CourseTable::make())
                    ->model(Course::class,'id','name')
                     ->width(3);

                $filter->equal('is_free')
                       ->select([0 => '否',1 => '是'])
                        ->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CourseChapter(), function (Show $show) {
            $show->field('id');
            $show->field('course_chapter_number_id');
            $show->field('courses_id');
            $show->field('name');
            $show->field('video_url');
            $show->field('video_poster');
            $show->field('is_free');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CourseChapter(), function (Form $form) {
            $form->display('id');
            $form->selectTable('course_id','课程')
                 ->from(CourseTable::make())
                 ->model(Course::class,'id','name')
                 ->required();
            $form->select('course_chapter_number_id','次序')
                 ->options(CourseChapterNumber::getOption())
                 ->required();

            $form->text('name')
                 ->maxLength(255)
                 ->required();
            $form->image('video_poster')
                 ->autoUpload()
                 ->uniqueName()
                 ->removable(false)
                 ->saveFullUrl()
                 ->required();

            $form->file('video_url')
                ->autoUpload()
                ->uniqueName()
                ->removable(false)
                ->saveFullUrl()
                ->required();


            $form->display('video_url','浏览视频')
                ->customFormat(function ($value){
                    $html = '<video width="320" height="240" controls>
    <source src="'.$value.'" type="video/mp4">
</video>';
                    return $html;
                });

            $form->radio('is_free')
                ->options([0 => '否',1 => '是'])
                ->default(0)
                ->required();
        });
    }
}
